col4 bit P2.7
col3 bit P2.6
col2 bit P2.5
col1 bit P2.4
row1 bit P2.3
row2 bit P2.2
row3 bit P2.1
row4 bit P2.0

keydata equ 70h
keybounc equ 71h
keyport equ P2
;
org 0h
mov P2,#11111111b ;isi P2 dengan 11111111
start: 	call keypad4x4 ;calling subroutine keypad4x4
	Mov A,keydata ;Copy keydata ke A
	Cjne A,#0FFh,send 
	;Bandingkan A dgn FF. Tidak sama, jump ke send.
	sjmp start ;LOOPING ke start
send: 	CPL A ;A = NOT A
	Mov P0,A ;Copy A ke P0 yang terhubung ke LED
	sjmp start ;LOOPING FOREVER PART 2
;
;==========================================
; subroutine scan keypad 1 column x 4 row
;==========================================
Keypad4x4:
	mov keybounc,#50 ;keybounc = 50
	mov keyport,#0FFh ;keyport=P2= FF
	clr col4 ;col4= P2.0 = 0
;
keyCOR:
	jb row4,keyMEN ;Key COR. Jump ke keyMEN,
	;jika row4= 1
	djnz keybounc,KeyCOR ;delay untuk bouncing
	mov keydata,#0Ah ;Data Output = 0Ah
ret
;
keyMEN:
	jb row3,keyUpA ; Key MEN
	djnz keybounc,keyMEN
	mov keydata,#0bh ;Data Output = 0bh
ret
;
keyUpA:
	jb row2,keyDnA ;
	djnz keybounc,keyUpA
	mov keydata,#0ch ; Data Output = 0ch
ret
;
keyDnA:
	jb row1,Nokey ;
	djnz keybounc,keyDnA
	mov keydata,#0dh ; Data Output = 0dh
ret

Nokey:mov keydata,#0FFh
ret
;=====================================================
;The end of Keypad 4x4 subroutine
;Start of delay subroutine
;(modifikasi program ini untuk simulasi di emulator)
;=====================================================
delay: 	mov R0,#0
	delay1:mov R2,#50
	djnz R2,$
	djnz R0,delay1
ret
end